var app = {
    
    pp_uid : '',
    db: undefined,
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },

    bindOtherEvents: function(){
      document.addEventListener("backbutton", this.onBackKeyDown, false);
      document.addEventListener("menubutton", this.onMenuKeyDown, false);
    },
    onBackKeyDown: function(){
      app.receivedEvent('onBackKeyDown');
      app.askQuit();
    },
    onMenuKeyDown: function(){
      app.receivedEvent('onMenuKeyDown');
      
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
         app.receivedEvent('onDeviceReady');
         app.bindOtherEvents();
         checkConnection();
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        console.log('web console Received Event: ' + id);
    },
    askQuit:function(){
        navigator.notification.confirm(
            '按确定退出程序!',  // message
            function(button) {
               if(button==1) navigator.app.exitApp(); //选择了确定才执行退出
            },              // callback to invoke with index of button pressed
            '确定要退出程序吗?',            // title
            '确定,取消'          // buttonLabels
      );
    }
};


$(function(){
   app.initialize();
});
function checkConnection() {
    var networkState = navigator.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.CELL]     = 'Cell generic connection';
    states[Connection.NONE]     = 'No network connection';

    app.receivedEvent('Connection type: ' + states[networkState]);
}